package com.jianyun.jyzs.widget;

import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.PopupWindow;


import com.jianyun.jyzs.R;
import com.jianyun.jyzs.activity.CrmActivity;
import com.jianyun.jyzs.adapter.CrmFilterAdapter;
import com.jianyun.jyzs.adapter.LeftAdapter;
import com.jianyun.jyzs.bean.Bean;
import com.jianyun.jyzs.dao.CrmFunnelDao;
import com.jianyun.jyzs.dao.EnNatureDao;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mlli on 2017/8/29.
 */

public class CrmConditionSearch {
    private CrmActivity context;
    private String enterpriseCode;
    private List<String> list_state = new ArrayList<>();
    private List<String> list_instudry = new ArrayList<>();
    private String province;
    private String city;
    private CheckBox appCrmSearchPartText;
    private PopupWindow popupWindow;
    private CrmFilterAdapter filterAdapter;

    public CrmConditionSearch(CrmActivity context, String enterpriseCode, CheckBox appCrmSearchPartText) {
        this.context = context;
        this.enterpriseCode = enterpriseCode;
        this.appCrmSearchPartText = appCrmSearchPartText;
    }
    public void show(View drop){
        View view = LayoutInflater.from(context).inflate(R.layout.popupwindow_crm_search_part, null);
        ListView leftlistview = (ListView) view.findViewById(R.id.leftList);
        RecyclerView rightlistview = (RecyclerView) view.findViewById(R.id.rightList);
        Button cancel = (Button) view.findViewById(R.id.cancelButton);
        Button search = (Button) view.findViewById(R.id.searchButton);
        View disk = view.findViewById(R.id.disclick);
        disk.setAlpha(0.5f);
        disk.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        popupWindow.dismiss();
                    }
                });
        List<String[]> leftData = new ArrayList<String[]>();
        leftData.add(new String[]{"1","客户状态"});
        leftData.add(new String[]{"0","客户性质"});
//        leftData.add(new String[]{"0","客户地区"});。。
        final List<String[]> stringList3 = new ArrayList<String[]>();
        stringList3.add(new String[]{"全部","省"});
        stringList3.add(new String[]{"全部","市"});

        CrmFunnelDao crmFunnelDao = new CrmFunnelDao(context);
        EnNatureDao industryDao = new EnNatureDao(context);
        list_instudry = industryDao.getEnNatureList2(enterpriseCode);
        list_state = crmFunnelDao.getStringList2(enterpriseCode);
        rightlistview.setLayoutManager(new LinearLayoutManager(context));
        if (searchonditionIndtury.size()<=0)for (String str:list_instudry
             ) {
            searchonditionIndtury.add(new Bean(str,false));
        }
        if (searchonditionState.size()<=0)for (String str:list_state
                ) {
            searchonditionState.add(new Bean(str,false));
        }

        filterAdapter = new CrmFilterAdapter(context, searchonditionState, searchonditionIndtury);
        rightlistview.setAdapter(filterAdapter);
        LeftAdapter leftAdapter = new LeftAdapter(context, leftData);
        leftAdapter.setLeftTagChangeListener(new LeftAdapter.LeftTagChangeListener() {
            @Override
            public void changeIndex(int index) {
                switch (index){
                    case 0:
                        filterAdapter.setData(0);
                        break;
                    case 1:
                        filterAdapter.setData(1);
                        break;
                    case 2:
                        break;
                }
            }
        });
        leftlistview.setAdapter(leftAdapter);



        popupWindow = new Solve7PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
                searchonditionState = new ArrayList<>();
                searchonditionIndtury = new ArrayList<>();
                for (String str:list_instudry
                        ) {
                    searchonditionIndtury.add(new Bean(str,false));
                }
                for (String str:list_state
                        ) {
                    searchonditionState.add(new Bean(str,false));
                }
            }
        });
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                searchonditionIndtury = filterAdapter.getSearchonditionIndtury();
                searchonditionState = filterAdapter.getSearchonditionState();
                List<String> strigs1 = new ArrayList<>();
                List<String> strigs2 = new ArrayList<>();
                for (Bean str: searchonditionIndtury
                     ) {
                    if (str.check)strigs1.add(str.name);
                }
                for (Bean str: searchonditionState
                        ) {
                    if (str.check)strigs2.add(str.name);
                }
                context.searchCondition(strigs1,strigs2);
                popupWindow.dismiss();
            }
        });
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(0x4D000000));
        popupWindow.update();
        popupWindow.showAsDropDown(drop);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                appCrmSearchPartText.setChecked(false);
            }
        });
    }
    private List<Bean> searchonditionIndtury = new ArrayList<>();
    private List<Bean> searchonditionState = new ArrayList<>();

    public void diss() {
        popupWindow.dismiss();
        appCrmSearchPartText.setChecked(false);
    }
}
